<template>
  <div class="box">
    <div class="top_back">
      <div></div>
      <div class="top_logo">
        <img src="../../../../assets/static/register/l1.png"
             alt=""/>
      </div>
      <div class="card">
        <div class="card_title">
          <img src="../../../../assets/static/login/r1.png" alt=""/>
          <div>Login</div>
          <img src="../../../../assets/static/login/r2.png" alt=""/>
        </div>
        <div class="card_Phone">
          <img src="../../../../assets/static/login/p.png" alt=""/>
          <div>{{ $t('home.Mobile') }}</div>
        </div>
        <div class="card_input">
          <!--          <div>-->
          <el-select class="inputIn" v-model="areacode" :placeholder="$t('Select Area Code')">
            <el-option v-for="item in areaList"
                       :key="item.ROW_NUMBER"
                       :value="item.code">{{ item.name }}
            </el-option>
          </el-select>
          <div>
            <input @input="onlyNum" v-model="mobile"/>
          </div>
        </div>
        <div class="col-wrapper" style="width:100%">
          <div class="card_btn" @click="sendCode">Get code</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {login, sendVerify} from '@/api/merbership/login'
import {setToken, setReferToken} from '@/utils/tokenTools'
import {govlist} from '@/api/merbership/userbank'
import '@/styles/elInput.scss'
import '@/styles/flax.css'

export default {
  data() {
    return {
      areacode: '+65',
      mobile: '',
      areaList: []
    }
  },
  activated() {
    this.govlist()
  },
  methods: {
    /**
     * 只能输入数字
     */
    onlyNum() {
      this.mobile = this.mobile.replace(/[^\d]/g, '')
    },
    // -------------- axios -----------------
    /**
     * 区号
     */
    govlist() {
      const pd = {}
      govlist(pd)
        .then(res => {
          if (res.data.code === 1) {
            this.areaList = res.data.data
          } else {
            this.$message.error(res.data.msg)
          }
        })
    },
    /**
     * 发送验证码
     */
    sendCode() {
      const pd = {
        phone: this.mobile,
        // phone: this.mobile,
        areacode: this.areacode
      }
      // var reg = /^0?1[3|4|5|6|7|8][0-9]\d{8}$/
      var reg = /\b\d{5,11}\b/g
      if (reg.test(this.mobile)) {
        if (this.areacode.length > 0) {
          this.sendVerify({
            d_code: pd.areacode,
            // phone: this.areaCode + this.phone,
            phone: pd.phone,
          })
        } else {
          this.$message.error('请选择区号')
        }
      } else {
        this.$message.error('手机号不正确')
      }
    },
    /**
     * 登录获取接口
     */
    login() {
      const pd = {
        mobile: this.account,
        code: this.password,
        areacode: this.areacode
      }
      login(this.$qs.stringify(pd))
        .then(res => {
          this.$message.success(res.data.msg)
          setReferToken(res.data.data.refer_token)
          if (setToken(res.data.data.token)) {
            this.$router.push({path: 'listhome'})
          }
        })
    },
    /**
     * 发送验证码
     */
    sendVerify(pd) {
      sendVerify(this.$qs.stringify(pd))
        .then(res => {
          if (res.data.code === 1) {
            this.$message.success(res.data.msg)
            this.$router.push({
              path: '/listregister', query: {
                phone: this.mobile,
                // phone: this.mobile,
                areacode: this.areacode
              }
            })
          } else {
            this.$message.error(res.data.msg)
          }
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.inputIn {
  width: 100%;
}

.box {
  height: 100vh;
  background-color: #fff;
}

.top_back {
  background-image: url(../../../../assets/static/register/b2.png);
  background-repeat: no-repeat;
  background-size: 100%;

  .el-icon-arrow-left {
    color: #FFFFFF;
    padding: 30px 15px;
  }

  .top_logo {
    text-align: center;

    img {
      width: 263px;
    }
  }

  .card {
    margin: 15px;
    padding: 20px;
    background-color: #fff;
    box-shadow: 0 0 15px #CCCCCC;
    border-radius: 10px;

    .card_title {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 23px;
      color: #65BE06;
      font-weight: bold;

      div {
        margin: 0 10px;
      }
    }

    .card_Phone {
      display: flex;
      align-items: center;
      font-size: 14px;
      color: #65BD06;
      font-weight: bold;
      margin: 36px 0 13px 0;

      img {
        width: 10px;
        margin-right: 6px;
      }
    }

    .card_input {

      div:nth-child(2) {
        padding: 15px;
        border-radius: 10px;
        box-shadow: 0 0 10px #ccc;
        margin: 14px 0;
        display: flex;
        align-items: center;
        font-size: 11px;

        select {
          width: 100%;
          appearance: none;
          -moz-appearance: none;
          -webkit-appearance: none;
          border: none;
          outline: none;
        }

        select::-ms-expand {
          display: none;
        }

        input {
          width: 100%;
          border: none;
          outline: none;
        }
      }
    }

    .card_btn {
      background-image: url(../../../../assets/static/register/b3.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      width: 294px;
      text-align: center;
      font-size: 14px;
      color: #FFFFFF;
      font-weight: bold;
      padding: 25px 0 35px 0;
    }
  }
}

</style>
<style>
.el-input__inner {
  border-color: transparent;
  width: 305.2px;
  outline: none;
  height: 48px;
  border-radius: 10px;
  box-shadow: 0 0 10px #ccc;
}

.el-select .el-input.is-focus .el-input__inner {
  border-color: transparent;
  border-radius: 10px;
  box-shadow: 0 0 10px #ccc;
}

.el-select:hover .el-input__inner {
  border-color: transparent;
  border-radius: 10px;
  box-shadow: 0 0 10px #ccc;
}
</style>
